home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWODUtil / Sources / FWSUBuff.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  5.2 KB  |  176 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:        FWAccBuf.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWSUBUFF_H
  13. #include "FWSUBuff.h"
  14. #endif
  15.  
  16. #ifndef FWPRIDEB_H
  17. #include "FWPriDeb.h"
  18. #endif
  19.  
  20. //========================================================================================
  21. //    RunTime Info
  22. //========================================================================================
  23.  
  24. #if FW_LIB_EXPORT_PRAGMAS
  25. #pragma lib_export on
  26. #endif
  27.  
  28. #ifdef FW_BUILD_MAC
  29. #pragma segment fwodutil
  30. #endif
  31.  
  32. //========================================================================================
  33. //    class FW_CStorageUnitBuffer
  34. //========================================================================================
  35.  
  36. //----------------------------------------------------------------------------------------
  37. //    FW_CStorageUnitBuffer::FW_CStorageUnitBuffer
  38. //
  39. //    This constructor just initializes some fields--no memory is actually allocated
  40. //    for the buffer until Initialize() is called.
  41. //----------------------------------------------------------------------------------------
  42.  
  43. FW_CStorageUnitBuffer::FW_CStorageUnitBuffer(long capacity) :
  44.     fBuffer(NULL),
  45.     fCapacity(capacity),
  46.     fPosition(0),
  47.     fValidBytes(0),
  48.     fType(kInvalid),
  49.     fInitialPosition(0)
  50. {
  51. }
  52.  
  53. //----------------------------------------------------------------------------------------
  54. //    FW_CStorageUnitBuffer::FW_CStorageUnitBuffer
  55. //
  56. //    The copy constructor creates a buffer of the same size as that being
  57. //    copied.  No data is copied into the new buffer.
  58. //----------------------------------------------------------------------------------------
  59.  
  60. FW_CStorageUnitBuffer::FW_CStorageUnitBuffer(const FW_CStorageUnitBuffer& otherBuffer) :
  61.     fBuffer(NULL),
  62.     fCapacity(otherBuffer.fCapacity),
  63.     fPosition(0),
  64.     fValidBytes(0),
  65.     fType(kInvalid),
  66.     fInitialPosition(0)
  67. {
  68. }
  69.  
  70. //----------------------------------------------------------------------------------------
  71. //    FW_CStorageUnitBuffer::~FW_CStorageUnitBuffer
  72. //----------------------------------------------------------------------------------------
  73.  
  74. FW_CStorageUnitBuffer::~FW_CStorageUnitBuffer()
  75. {
  76.     if (fBuffer)
  77.         delete [] fBuffer;
  78. }
  79.  
  80. //----------------------------------------------------------------------------------------
  81. //    FW_CStorageUnitBuffer::FW_CStorageUnitBuffer
  82. //
  83. //    The assignment operator only changes the size of the buffer to match that of
  84. //    the buffer it is being assigned to.  No data is copied.
  85. //----------------------------------------------------------------------------------------
  86.  
  87. FW_CStorageUnitBuffer& FW_CStorageUnitBuffer::operator=(const FW_CStorageUnitBuffer& otherBuffer)
  88. {
  89.     if (this != &otherBuffer)
  90.     {
  91.         if (fCapacity != otherBuffer.fCapacity)
  92.         {
  93.             delete [] fBuffer;
  94.             
  95.             fBuffer = NULL;
  96.             fCapacity = otherBuffer.fCapacity;
  97.             fType = kInvalid;
  98.         }
  99.     }
  100.  
  101.     return (*this);
  102. }
  103.  
  104. //----------------------------------------------------------------------------------------
  105. //    FW_CStorageUnitBuffer::Initialize
  106. //----------------------------------------------------------------------------------------
  107.  
  108. void FW_CStorageUnitBuffer::Initialize(EBufferKind kind, long validBytes, long filePosition)
  109. {
  110.     fPosition = 0;
  111.     fValidBytes = validBytes;
  112.     fType = kind;
  113.     fInitialPosition = filePosition;
  114.  
  115.     if (!fBuffer && fType != kInvalid)
  116.         fBuffer = new char[fCapacity];    
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120. //    FW_CStorageUnitBuffer::ReadPeek
  121. //----------------------------------------------------------------------------------------
  122.  
  123. const void* FW_CStorageUnitBuffer::ReadPeek(long& availableReadBytes)
  124. {
  125.     if (fType != kReadPeek)
  126.     {
  127.         availableReadBytes = 0;
  128.         return NULL;
  129.     }
  130.     
  131.     availableReadBytes = fValidBytes - fPosition;
  132.     FW_ASSERT(availableReadBytes >= 0);
  133.     return fBuffer + fPosition;
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. //    FW_CStorageUnitBuffer::ReadPeekAdvance
  138. //----------------------------------------------------------------------------------------
  139.  
  140. void FW_CStorageUnitBuffer::ReadPeekAdvance(long bytesRead)
  141. {
  142.     FW_ASSERT(fType == kReadPeek);
  143.     FW_ASSERT(bytesRead >= 0);
  144.     fPosition += bytesRead;
  145.     FW_ASSERT(fPosition <= fValidBytes);
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. //    FW_CStorageUnitBuffer::WritePeek
  150. //----------------------------------------------------------------------------------------
  151.  
  152. void* FW_CStorageUnitBuffer::WritePeek(long& availableWriteBytes)
  153. {
  154.     if (fType != kWritePeek)
  155.     {
  156.         availableWriteBytes = 0;
  157.         return NULL;
  158.     }
  159.     
  160.     availableWriteBytes = fValidBytes - fPosition;
  161.     FW_ASSERT(availableWriteBytes >= 0);
  162.     return fBuffer + fPosition;
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. //    FW_CStorageUnitBuffer::WritePeekAdvance
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void FW_CStorageUnitBuffer::WritePeekAdvance(long bytesWritten)
  170. {
  171.     FW_ASSERT(fType == kWritePeek);
  172.     FW_ASSERT(bytesWritten >= 0);
  173.     fPosition += bytesWritten;
  174.     FW_ASSERT(fPosition <= fValidBytes);
  175. }
  176.